home *** CD-ROM | disk | FTP | other *** search
/ PC Advisor 2007 June / PC Advisor 2007 June.iso / FULL / OPENOFFICE / openoffice.exe / openofficeorg4.cab / mailmerge.py < prev    next >
Text File  |  2006-11-30  |  11KB  |  344 lines

  1. #!/bin/python
  2.  
  3. # Caolan McNamara caolanm@redhat.com
  4. # a simple email mailmerge component
  5.  
  6. # manual installation for hackers, not necessary for users
  7. # cp mailmerge.py /usr/lib/openoffice.org2.0/program
  8. # cd /usr/lib/openoffice.org2.0/program
  9. # ./unopkg add --shared mailmerge.py
  10. # edit ~/.openoffice.org2/user/registry/data/org/openoffice/Office/Writer.xcu
  11. # and change EMailSupported to as follows...
  12. #  <prop oor:name="EMailSupported" oor:type="xs:boolean">
  13. #   <value>true</value>
  14. #  </prop>
  15.  
  16. import unohelper
  17. import uno
  18.  
  19. #to implement com::sun::star::mail::XMailServiceProvider
  20.  
  21. from com.sun.star.mail import XMailServiceProvider
  22. from com.sun.star.mail import XMailService
  23. from com.sun.star.mail import XSmtpService
  24. from com.sun.star.mail import XConnectionListener
  25. from com.sun.star.mail import XAuthenticator
  26. from com.sun.star.mail import XMailMessage
  27. from com.sun.star.mail.MailServiceType import SMTP
  28. from com.sun.star.mail.MailServiceType import POP3
  29. from com.sun.star.mail.MailServiceType import IMAP
  30. from com.sun.star.uno import XCurrentContext
  31. from com.sun.star.lang import IllegalArgumentException
  32. from com.sun.star.lang import EventObject
  33. from com.sun.star.mail import SendMailMessageFailedException
  34.  
  35. from email.MIMEBase import MIMEBase
  36. from email.Message import Message
  37. from email import Encoders
  38. from email.MIMEMultipart import MIMEMultipart
  39.  
  40. import sys, smtplib, imaplib, poplib
  41.  
  42. dbg = False
  43.  
  44. class PyMailSMTPService(unohelper.Base, XSmtpService):
  45.     def __init__( self, ctx ):
  46.         self.ctx = ctx
  47.         self.listeners = []
  48.         self.supportedtypes = ('Insecure', 'Ssl')
  49.         self.server = None
  50.         self.connectioncontext = None
  51.         self.notify = EventObject()
  52.         if dbg:
  53.             print >> sys.stderr, "PyMailSMPTService init"
  54.     def addConnectionListener(self, xListener):
  55.         if dbg:
  56.             print >> sys.stderr, "PyMailSMPTService addConnectionListener"
  57.         self.listeners.append(xListener)
  58.     def removeConnectionListener(self, xListener):
  59.         if dbg:
  60.             print >> sys.stderr, "PyMailSMPTService removeConnectionListener"
  61.         self.listeners.remove(xListener)
  62.     def getSupportedConnectionTypes(self):
  63.         if dbg:
  64.             print >> sys.stderr, "PyMailSMPTService getSupportedConnectionTypes"
  65.         return self.supportedtypes
  66.     def connect(self, xConnectionContext, xAuthenticator):
  67.         self.connectioncontext = xConnectionContext
  68.         if dbg:
  69.             print >> sys.stderr, "PyMailSMPTService connect"
  70.         server = xConnectionContext.getValueByName("ServerName")
  71.         if dbg:
  72.             print >> sys.stderr, server
  73.         port = xConnectionContext.getValueByName("Port")
  74.         if dbg:
  75.             print >> sys.stderr, port
  76.         self.server = smtplib.SMTP(server, port)
  77.         if dbg:
  78.             self.server.set_debuglevel(1)
  79.         connectiontype = xConnectionContext.getValueByName("ConnectionType")
  80.         if dbg:
  81.             print >> sys.stderr, connectiontype
  82.         if connectiontype == 'Ssl':
  83.             self.server.starttls()
  84.  
  85.         user = xAuthenticator.getUserName()
  86.         password = xAuthenticator.getPassword()
  87.         if user != '':
  88.             if dbg:
  89.                 print >> sys.stderr, 'Logging in, username of', user
  90.             self.server.login(user, password)
  91.  
  92.         for listener in self.listeners:
  93.             listener.connected(self.notify)
  94.     def disconnect(self):
  95.         if dbg:
  96.             print >> sys.stderr, "PyMailSMPTService disconnect"
  97.         if self.server:
  98.             self.server.quit()
  99.             self.server = None
  100.         for listener in self.listeners:
  101.             listener.disconnected(self.notify)
  102.     def isConnected(self):
  103.         if dbg:
  104.             print >> sys.stderr, "PyMailSMPTService isConnected"
  105.         return self.server != None
  106.     def getCurrentConnectionContext(self):
  107.         if dbg:
  108.             print >> sys.stderr, "PyMailSMPTService getCurrentConnectionContext"
  109.         return self.connectioncontext
  110.     def sendMailMessage(self, xMailMessage):
  111.         COMMASPACE = ', '
  112.  
  113.         if dbg:
  114.             print >> sys.stderr, "PyMailSMPTService sendMailMessage"
  115.         recipients = xMailMessage.getRecipients()
  116.         sender = xMailMessage.SenderAddress
  117.         subject = xMailMessage.Subject
  118.         ccrecipients = xMailMessage.getCcRecipients()
  119.         bccrecipients = xMailMessage.getBccRecipients()
  120.         if dbg:
  121.             print >> sys.stderr, "PyMailSMPTService subject", subject
  122.             print >> sys.stderr, "PyMailSMPTService from", sender
  123.             print >> sys.stderr, "PyMailSMPTService send to", recipients
  124.  
  125.         attachments = xMailMessage.getAttachments()
  126.  
  127.         content = xMailMessage.Body
  128.         flavors = content.getTransferDataFlavors()
  129.         flavor = flavors[0]
  130.         if dbg:
  131.             print >> sys.stderr, "PyMailSMPTService mimetype is", flavor.MimeType
  132.         textbody = content.getTransferData(flavor)
  133.  
  134.         textmsg = Message()
  135.         textmsg['Content-Type'] = flavor.MimeType
  136.             textmsg['MIME-Version'] = '1.0'
  137.         textmsg.set_payload(textbody.encode('utf-8'))
  138.  
  139.         if (len(attachments)):
  140.             msg = MIMEMultipart()
  141.             msg.epilogue = ''
  142.             msg.attach(textmsg)
  143.         else:
  144.             msg = textmsg
  145.  
  146.         msg['Subject'] = subject
  147.         msg['From'] = sender
  148.         msg['To'] = COMMASPACE.join(recipients)
  149.         if len(ccrecipients):
  150.             msg['Cc'] = COMMASPACE.join(ccrecipients)
  151.         if xMailMessage.ReplyToAddress != '':
  152.             msg['Reply-To'] = xMailMessage.ReplyToAddress
  153.         msg['X-Mailer'] = "OpenOffice.org 2.0 via Caolan's mailmerge component"
  154.  
  155.         for attachment in attachments:
  156.             content = attachment.Data
  157.             flavors = content.getTransferDataFlavors()
  158.             flavor = flavors[0]
  159.             ctype = flavor.MimeType
  160.             maintype, subtype = ctype.split('/', 1)
  161.             msgattachment = MIMEBase(maintype, subtype)
  162.             data = content.getTransferData(flavor)
  163.             msgattachment.set_payload(data)
  164.             Encoders.encode_base64(msgattachment)
  165.             msgattachment.add_header('Content-Disposition', 'attachment', \
  166.                 filename=attachment.ReadableName)
  167.             msg.attach(msgattachment)
  168.  
  169.         uniquer = {}
  170.         for key in recipients:
  171.             uniquer[key] = True
  172.         if len(ccrecipients):
  173.             for key in ccrecipients:
  174.                 uniquer[key] = True
  175.         if len(bccrecipients):
  176.             for key in bccrecipients:
  177.                 uniquer[key] = True
  178.         truerecipients = uniquer.keys()
  179.  
  180.         if dbg:
  181.             print >> sys.stderr, "PyMailSMPTService recipients are", truerecipients
  182.  
  183.         self.server.sendmail(sender, recipients, msg.as_string())
  184.  
  185. class PyMailIMAPService(unohelper.Base, XMailService):
  186.     def __init__( self, ctx ):
  187.         self.ctx = ctx
  188.         self.listeners = []
  189.         self.supportedtypes = ('Insecure', 'Ssl')
  190.         self.server = None
  191.         self.connectioncontext = None
  192.         if dbg:
  193.             print >> sys.stderr, "PyMailIMAPService init"
  194.     def addConnectionListener(self, xListener):
  195.         if dbg:
  196.             print >> sys.stderr, "PyMailIMAPService addConnectionListener"
  197.         self.listeners.append(xListener)
  198.     def removeConnectionListener(self, xListener):
  199.         if dbg:
  200.             print >> sys.stderr, "PyMailIMAPService removeConnectionListener"
  201.         self.listeners.remove(xListener)
  202.     def getSupportedConnectionTypes(self):
  203.         if dbg:
  204.             print >> sys.stderr, "PyMailIMAPService getSupportedConnectionTypes"
  205.         return self.supportedtypes
  206.     def connect(self, xConnectionContext, xAuthenticator):
  207.         if dbg:
  208.             print >> sys.stderr, "PyMailIMAPService connect"
  209.  
  210.         self.connectioncontext = xConnectionContext
  211.         server = xConnectionContext.getValueByName("ServerName")
  212.         if dbg:
  213.             print >> sys.stderr, server
  214.         port = xConnectionContext.getValueByName("Port")
  215.         if dbg:
  216.             print >> sys.stderr, port
  217.         connectiontype = xConnectionContext.getValueByName("ConnectionType")
  218.         if dbg:
  219.             print >> sys.stderr, connectiontype
  220.         print >> sys.stderr, "BEFORE"
  221.         if connectiontype == 'Ssl':
  222.             self.server = imaplib.IMAP4_SSL(server, port)
  223.         else:
  224.             self.server = imaplib.IMAP4(server, port)
  225.         print >> sys.stderr, "AFTER"
  226.             
  227.         user = xAuthenticator.getUserName()
  228.         password = xAuthenticator.getPassword()
  229.         if user != '':
  230.             if dbg:
  231.                 print >> sys.stderr, 'Logging in, username of', user
  232.             self.server.login(user, password)
  233.  
  234.         for listener in self.listeners:
  235.             listener.connected(self.notify)
  236.     def disconnect(self):
  237.         if dbg:
  238.             print >> sys.stderr, "PyMailIMAPService disconnect"
  239.         if self.server:
  240.             self.server.logout()
  241.             self.server = None
  242.         for listener in self.listeners:
  243.             listener.disconnected(self.notify)
  244.     def isConnected(self):
  245.         if dbg:
  246.             print >> sys.stderr, "PyMailIMAPService isConnected"
  247.         return self.server != None
  248.     def getCurrentConnectionContext(self):
  249.         if dbg:
  250.             print >> sys.stderr, "PyMailIMAPService getCurrentConnectionContext"
  251.         return self.connectioncontext
  252.  
  253. class PyMailPOP3Service(unohelper.Base, XMailService):
  254.     def __init__( self, ctx ):
  255.         self.ctx = ctx
  256.         self.listeners = []
  257.         self.supportedtypes = ('Insecure', 'Ssl')
  258.         self.server = None
  259.         self.connectioncontext = None
  260.         if dbg:
  261.             print >> sys.stderr, "PyMailPOP3Service init"
  262.     def addConnectionListener(self, xListener):
  263.         if dbg:
  264.             print >> sys.stderr, "PyMailPOP3Service addConnectionListener"
  265.         self.listeners.append(xListener)
  266.     def removeConnectionListener(self, xListener):
  267.         if dbg:
  268.             print >> sys.stderr, "PyMailPOP3Service removeConnectionListener"
  269.         self.listeners.remove(xListener)
  270.     def getSupportedConnectionTypes(self):
  271.         if dbg:
  272.             print >> sys.stderr, "PyMailPOP3Service getSupportedConnectionTypes"
  273.         return self.supportedtypes
  274.     def connect(self, xConnectionContext, xAuthenticator):
  275.         if dbg:
  276.             print >> sys.stderr, "PyMailPOP3Service connect"
  277.  
  278.         self.connectioncontext = xConnectionContext
  279.         server = xConnectionContext.getValueByName("ServerName")
  280.         if dbg:
  281.             print >> sys.stderr, server
  282.         port = xConnectionContext.getValueByName("Port")
  283.         if dbg:
  284.             print >> sys.stderr, port
  285.         connectiontype = xConnectionContext.getValueByName("ConnectionType")
  286.         if dbg:
  287.             print >> sys.stderr, connectiontype
  288.         print >> sys.stderr, "BEFORE"
  289.         if connectiontype == 'Ssl':
  290.             self.server = poplib.POP3_SSL(server, port)
  291.         else:
  292.             self.server = poplib.POP3(server, port)
  293.         print >> sys.stderr, "AFTER"
  294.             
  295.         user = xAuthenticator.getUserName()
  296.         password = xAuthenticator.getPassword()
  297.         if dbg:
  298.             print >> sys.stderr, 'Logging in, username of', user
  299.         self.server.user(user)
  300.         self.server.pass_(user, password)
  301.  
  302.         for listener in self.listeners:
  303.             listener.connected(self.notify)
  304.     def disconnect(self):
  305.         if dbg:
  306.             print >> sys.stderr, "PyMailPOP3Service disconnect"
  307.         if self.server:
  308.             self.server.quit()
  309.             self.server = None
  310.         for listener in self.listeners:
  311.             listener.disconnected(self.notify)
  312.     def isConnected(self):
  313.         if dbg:
  314.             print >> sys.stderr, "PyMailPOP3Service isConnected"
  315.         return self.server != None
  316.     def getCurrentConnectionContext(self):
  317.         if dbg:
  318.             print >> sys.stderr, "PyMailPOP3Service getCurrentConnectionContext"
  319.         return self.connectioncontext
  320.  
  321. class PyMailServiceProvider(unohelper.Base, XMailServiceProvider):
  322.     def __init__( self, ctx ):
  323.         if dbg:
  324.             print >> sys.stderr, "PyMailServiceProvider init"
  325.         self.ctx = ctx
  326.     def create(self, aType):
  327.         if dbg:
  328.             print >> sys.stderr, "PyMailServiceProvider create with", aType
  329.         if aType == SMTP:
  330.             return PyMailSMTPService(self.ctx);
  331.         elif aType == POP3:
  332.             return PyMailPOP3Service(self.ctx);
  333.         elif aType == IMAP:
  334.             return PyMailIMAPService(self.ctx);
  335.         else:
  336.             print >> sys.stderr, "PyMailServiceProvider, unknown TYPE", aType
  337.  
  338. # pythonloader looks for a static g_ImplementationHelper variable
  339. g_ImplementationHelper = unohelper.ImplementationHelper()
  340. g_ImplementationHelper.addImplementation( \
  341.     PyMailServiceProvider, "org.openoffice.pyuno.MailServiceProvider",
  342.         ("com.sun.star.mail.MailServiceProvider",),)
  343.  
  344.